<?php

// $Id$

require_once($WSC->get_site_inc_dir().'modules/tangra_cms/_sys/tcms_admin_web_page_wed_ua.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/ci/tcms_ci_dbc.class.php');


class CI_Publish_WP extends TCMS_Admin_Web_Page_WED_UA {
	protected function init_transit_vars() {
		$this->add_transit_var('vpage');
		$this->add_transit_var('category');
	}


	public function init() {
		parent::init();

		$publish_event = new Web_Event_Simple_Int('ci', Tangra_Parameter_Method::GET, 'ci');
		$ret_event = new Web_Event_Simple('ret', Tangra_Parameter_Method::GET, 'ret');
		$publish_comp = new Web_Event_Composite('publish_event');
		$publish_comp->add_wes($publish_event);
		$publish_comp->add_wes($ret_event);

		$this->wed->add_wed_event_action_pair($publish_comp, 'publish_event');
	}


	protected function publish_event($params) {
		$dbc = $this->get_dbc();

		try {
			$dbc->start_trans();
			TCMS_CI_DBC::publish($dbc, $params['ci']);
			$dbc->complete_trans();
		} catch (Exception $e) {
			$dbc->fail_trans();
			$dbc->complete_trans();
			throw $e;
		}

		$view = $this->create_redirect_view($params['ret'], array('manage' => $params['ci']));

		return $view;
	}


	protected function default_event() {
		throw new Tangra_Module_Exception('Expecting "publish" and "ret" parameters.');
	}
}